﻿var allCity = "/area/query/selfCity.do";
var areaDepart='/report/status/area.do';            			//根据地市查询部门信息方法
var modifyState='/report/status/cancel/confirm.do';           	//批准退回接口(修改上报状态为上报中)
var getReport='/forms/list.do'    					//获得报表信息接口
var countyList="/area/query/county.do";//获取区县信息
var action_self_view = "/area/query/self.do";

var page_level="";
var city_html =
    '<label>地市选择：</label>'
    +'<input class="easyui-combobox" name="cityCollect_citySelect" id="cityCollect_citySelect" size="10">';
var county_html =
    '<label>区县选择：</label>'
    + '<input class="easyui-combobox" name="countySelect" id="countySelect" size="10" style="width: 150px;">';

var auditState=4;			//需要上级部门审批的状态(申请退回状态)
var departIds='';
var page=1;
var pageSize=30;
var isEnd=false;

function loadCountyYear(){
	$('#yearSelect').combobox({
		onLoadSuccess: function () {
			var nowYear = formatDate(new Date(), "yyyy");
			$("#yearSelect").combobox("setValue", nowYear-1);
		},
		onSelect: function(rec){
			query();
		}
	});
}

//------------------------------------------------------------------菜单 start-------------------------------------------------------------
//获取用户所拥有的二级下拉列表菜单
function loadAreaView(){
    $.getJSON(action_self_view, function (json) {

        var code = json.code;
        if(code!=200){
            alert("网络不好啊！");
            return;
        }
        var data = json.data;
        var level = data.level;
        if(level === null){
            alert("用户权限配置不合理，请联系系统管理员！");
            return;
        }
        page_level = level;

        //画页面
        //根据不同的用户权限，控制用户页面显示几级菜单
        if(level=="province"||level=="city"){
            $("#insert_html").html(city_html+county_html);
            loadCombox();
        }else if(level=="county"){
            $("#insert_html").html(county_html);

            //加载区县(只有区县权限的用户)下拉列表
            loadCounty(host_path_action_area_selfCity, data.id);
        }

    }, "json");
}

//加载地市下拉列表
function loadCombox(){
	$.get(allCity, function (json) {
		$('#cityCollect_citySelect').combobox({    
			data:json.data,
		    valueField:'id',    
		    textField:'name',
		    onLoadSuccess: function () {
			      var a = $('#cityCollect_citySelect').combobox('getData');
			      if (a.length > 0) {
			        	$('#cityCollect_citySelect').combobox('select', a[0].id);
			      }
			},
			onSelect: function(rec){
				loadCounty(countyList, rec.id);
	        }
		});  
	}, "json");
}

//加载区县下拉列表
function loadCounty(action_url, id){
	$.get(action_url, {"areaid":id}, function (json) {
		  $('#countySelect').combobox({
		    async:false,
		    method:'GET',
		    data:json.data,
		    editable:true,
		    valueField:'id',
		    textField:'name',
		    onLoadSuccess: function () {
			      var a = $('#countySelect').combobox('getData');
			      if (a.length > 0) {
			        $('#countySelect').combobox('select', a[0].id);
			      }
		    },
		    onSelect:function (city) {
		    	 query();
		    }
		  })
		}, "json");
}
//------------------------------------------------------------------菜单 end-------------------------------------------------------------
function loadForm(){
	$('#formid').combobox({  
		 onSelect: function(rec){
			 query();
         }
	});
}

function loadGRID(){
	//加载部门列表
	var iHeight = document.documentElement.clientHeight;
	$('#depart_Grid').datagrid({
		width: '100%',
		height:iHeight-80,
		toolbar:'#departBtn',
		fitColumns: true,
		striped: true,
		checkOnSelect:false,
		selectOnCheck:false,
		singleSelect:true,
		pagination:true,
        remoteSort:false,//表示前端排序不请求服务器
		columns:getShowColumns()
	});
}

//部门列表清空缓存
function clearDGRID(){
	$('#depart_Grid').datagrid({
			data:blankLine("depart_Grid",28)
	});
	var depart_pg = $("#depart_Grid").datagrid("getPager");   
	$(depart_pg).pagination({
        total: 0,
        pageList: [30,50,100,500],
        beforePageText: '第',
        afterPageText: '页    共 ' + 0 + ' 页',
        displayMsg: '当前显示' + 0 + '-' + 0 + ' 条记录 共 ' + 0 + ' 条记录',
    });
}

//根据地市查询部门信息
function query(){
	var yearSelect=$("#yearSelect").combobox("getValue");    //年份
	var citySelect=$("#countySelect").combobox("getValue");    //地市
	var formid=$("#formid").combobox('getValue');   	//报表（妇儿、社会）
	
	$.get(areaDepart, {'year':yearSelect,'areaid':citySelect,'formsid':formid,'page':page,'pageSize':50},function (data) {
		if(data.code==200){
			var data  = data["data"].results;
			if(data==''||data==null||data==undefined||data=='[]'||data==[]){
				clearData();
				return;
			}
			load_jindutiao(data);
			var iHeight = document.documentElement.clientHeight;
			$('#depart_Grid').datagrid({
	            width: '100%',
	            height:iHeight-80,
	            pagination:true,
                pageList: [30,50,100,500],
                data:data.slice(0,30),
	            onSelect:function(index, row){
	            	// getTargetBydepart(row.departid,page,pageSize);
	            },
	            onLoadSuccess:function(){
	            	isEnd=true;
	            },
				onCheckAll:function(index, row){
					oncheckrows(index);
				},
				onCheck:function(index, row) {
					oncheckrows(index);
				}
	        });
			
			var pager = $("#depart_Grid").datagrid("getPager");  
            pager.pagination({  
                total:data.length,  
                onSelectPage:function (pageNo, pageSize) {  
                    var start = (pageNo - 1) * pageSize;  
                    var end = start + pageSize;  
                    $("#depart_Grid").datagrid("loadData", data.slice(start, end));  
                    pager.pagination('refresh', {  
                        total:data.length,  
                        pageNumber:pageNo  
                    });  
                }  
            });  
            
			$('#depart_Grid').datagrid('selectRow', 0);
		}else{	
			loadGRID();
			clearDGRID();
		}		
	}, "json");
}

//有小数位,最多4位
function isNumber(str){
	if(str==null||str.length<1){
		return false;
	}
	var reg= "^\\d+(\\.\\d{1,4})?$";
	var regExp = new RegExp(reg);
	return regExp.test(str); 
}


//修改部门上报状态(审批改为2 申请退回为4)
function modifyStates(departid,year){
	var formid=$("#formid").combobox("getValue");
	 $.ajax({
		 url:modifyState,
		 data:{'depart':departid,'year':year,"formsid":formid},
		 type: 'GET', 
		 dataType:"json",
		 contentType:"application/json;charset=UTF-8",
	     success: function (data) {
			if(data.success){
				$.messager.alert('系统提示', '操作成功！','info');
				query();
			}
	     },
	     error: function (xhr) {
	    	 $.messager.alert('系统提示', '操作失败！','info');
	     }
	 });
}

//动态构建空白行JSON数据
function blankLine(grid,count){
	 var opts = $('#'+grid).datagrid('getColumnFields'); 
	 var data_s=[];
	 for (var i = 0; i < count; i++) {
		 var data={};
		 for (var j = 0; j < opts.length; j++) {
			 data[opts[j]]='';
		 }
		 data_s.push(data);
	 }
	 return data_s;
}

$(function(){
	loadGRID();				//加载表格
	loadCountyYear();				//加载年份
	// loadCombox();
    loadAreaView();			//加载地域
	loadForm();				//加载报表
});

window.onresize = function () {
	var iHeight = document.documentElement.clientHeight;
	$("#depart_Grid").datagrid('resize',{height:iHeight-80+'px'});
}